import { request } from "../../request/index.js";
import regeneratorRuntime from '../../lib/runtime/runtime';
Page({
    data: {
        // 左边餐厅列表
        canteenList: [],
        // 右边窗口列表
        windowList: [],
        // 左侧菜单被点击项的索引
        currentIndex: 0,
        // 右侧内容滚动条与顶部的距离
        scrollTop: 0
    },
    // 接口返回数据
    Canteens: [],
    onLoad: function (options) {
        /**
         * 1.先判断本地缓存是否有旧的数据
         * 2.没有旧数据则直接发送请求
         * 3.有旧数据，若没有过期，则直接使用
         */
        // 获取本地缓存数据
        const Canteens = wx.getStorageSync('canteens');
        // 判断是否存在本地缓存数据以及数据是否过期（过期时间5分钟）
        if (!Canteens || Date.now() - Canteens.time > 1000 * 60 * 5) {
            this.getCanteens()
        } else {
            this.Canteens = Canteens.data;
            let canteenList = this.Canteens.map(v => v.canteenName);
            let windowList = this.Canteens[0].shopWindowList;
            this.setData({
                canteenList,
                windowList
            })
        }
    },
    // 获取餐厅数据
    async getCanteens() {
        // request({
        //     url: "/canteens/queryCanteen"
        // }).then(res => {
        //     this.Canteens = res.data.data;
        //     // 将数据存储到本地缓存
        //     wx.setStorageSync('canteens', {
        //         time: Date.now(),
        //         data: this.Canteens
        //     })
        //     // 构造左侧大菜单
        //     let canteenList = this.Canteens.map(v => v.canteenName);
        //     let windowList = this.Canteens[0].shopWindowList;
        //     this.setData({
        //         canteenList,
        //         windowList
        //     })
        // })
        const res = await request({
            url: "/canteens/queryCanteen"
        });
        this.Canteens = res.data.data;
        wx.setStorageSync('canteens', {
            time: Date.now(),
            data: this.Canteens
        })
        let canteenList = this.Canteens.map(v => v.canteenName);
        let windowList = this.Canteens[0].shopWindowList;
        this.setData({
            canteenList,
            windowList
        })
    },
    // 左侧菜单点击事件
    handleItemTap(e) {
        /*
            1.获取被点击标题的索引
            2.给 data 中的 currentIndex 赋值
         */
        const {
            index
        } = e.currentTarget.dataset;
        let windowList = this.Canteens[index].shopWindowList;
        this.setData({
            currentIndex: index,
            windowList,
            scrollTop: 0
        })
    }
})